home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5346 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news2.ios.com!usenet
  2. From: vlad@gramercy.ios.com (Vlastimil Adamovsky)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Derived classes inter-referencing each other
  5. Date: Sat, 03 Feb 1996 22:32:52 GMT
  6. Organization: Internet Online Services
  7. Message-ID: <4f0n91$a3b@news2.ios.com>
  8. References: <4em3ta$2p0@delphi.cs.ucla.edu>
  9. NNTP-Posting-Host: ppp-47.ts-7.hck.idt.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. geoff@ficus.cs.ucla.edu (Geoff Kuenning) wrote:
  13.  
  14. >My problem hasn't gotten quite this bad, so I was able to solve it
  15. >by re-ordering declarations.  But I started to wonder how one would
  16. >handle the more complex case.
  17.  
  18. >Suppose you have a pair of derived classes, foo and bar, derived from
  19. >foobase and barbase respectively.  The bases can be declared like
  20. >this:
  21.  
  22. >    class foobase {
  23. >        virtual barbase& givebar();
  24. >    };
  25. >    class barbase {
  26. >        virtual foobase& givefoo();
  27. >    };
  28.  
  29. >So far, so good.  Now we declare the derived classes.  However, we'd
  30. >like givebar() to return the corresponding derived class:
  31.  
  32. >    class foo : public foobase {
  33. >        virtual bar& givebar();
  34. >    };
  35.  
  36. >Oops!  Can't do that, because the compiler doesn't yet know that bar
  37. >is derived from barbase.  I tried putting a null declaration before foo:
  38.  
  39. >    class bar : public barbase;
  40.  
  41. >but unsurprisingly the compiler wasn't too happy with this.  The only
  42. >solution I can come up with is to declare foo "incorrectly":
  43.  
  44. >    class foo : public foobase {
  45. >        virtual barbase& givebar();
  46. >    };
  47. >    class bar : public barbase {
  48. >        virtual foo& givefoo();    // This is asymmetric but correct
  49. >    };
  50.  
  51. >and then use typecasts to get the result of givebar() to be of type bar.
  52.  
  53. >Anybody got a better solution?
  54. >-- 
  55. >    Geoff Kuenning    g.kuenning@ieee.org    geoff@ITcorp.com
  56. >    http://www.cs.ucla.edu/ficus-members/geoff/
  57.  
  58. Unless there is some special reason behind your code, you could use a
  59. pointer instead a reference. Then, it shoud work with a forward
  60. declarations.
  61.  
  62.  
  63.  
  64. *******************************************
  65. *    Vlastimil Adamovsky                  *
  66. * Smalltalk, C++ and Envelop development  *
  67. *******************************************
  68.  
  69.